package com.flea.model;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.Table;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.LinkedHashSet;
import java.util.Set;

@Table
@Data
public class Order {
    @Id
    private Integer id;
    private Integer buyerId;
    private Integer sellerId;
    private BigDecimal totalAmount;
    private BigDecimal shippingFee;
    private String shippingAddress;
    private Instant createTime;
    private Instant updateTime;
    //HashSet
    //LinkedHashSet
    //TreeSet
    @MappedCollection(idColumn = "order_id")
    private Set<OrderItem> orderItemSet = new LinkedHashSet<>();


    public void createOrderItem(Product product, short quantity) {
        OrderItem orderItem = new OrderItem();
        orderItem.setProductId(product.getId());
        orderItem.setUnitPrice(product.getCurrentPrice());
        orderItem.setQuantity(quantity);
        // currentPrice * quantity
        orderItem.setTotalPrice(product.getCurrentPrice().multiply(BigDecimal.valueOf(quantity)));

        orderItemSet.add(orderItem);
    }
}
